package com.csu.backmanagesys.Controller;


import com.csu.backmanagesys.Domain.Category;
import com.csu.backmanagesys.Service.CategoryService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.util.Base64;
import java.util.List;

@RequestMapping("/Category")
@Controller
public class CategoryController {
    @Autowired
    public CategoryService categoryService;

    @GetMapping("")
    public ModelAndView category(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("category" , categoryService.selectCategory());
        modelAndView.setViewName("category");
        return modelAndView;
    }

    @GetMapping("/selectCategory")
    public ModelAndView selectProduct(ModelAndView modelAndView,String search){
        modelAndView.addObject("category",categoryService.fuzzySelectCategory(search));
        modelAndView.setViewName("category");
        return modelAndView;
    }

    @PostMapping("/updateCategory")
    public ModelAndView updateCategory(HttpServletRequest req){
        String catId = req.getParameter("ca_tid");
        String enCatId = req.getParameter("en_catid");
        System.out.println("修改category"+catId+enCatId);
        categoryService.updateCategory(catId,enCatId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("category",categoryService.selectCategory());
        modelAndView.setViewName("category");
        return modelAndView;
    }

    @PostMapping("/insertCategory")
    public ModelAndView insertCategory(HttpServletRequest req){
        ModelAndView modelAndView = new ModelAndView();
        String catId = req.getParameter("ca_tid2");
        String enCatId = req.getParameter("en_catid2");
        if(catId.length()!=0 && enCatId.length()!=0){
            System.out.println("新增category"+catId+enCatId);
            categoryService.insertCategory(catId,enCatId,"");
            modelAndView.addObject("category" , categoryService.selectCategory());
            modelAndView.setViewName("category");
            return modelAndView;
        }
        modelAndView.addObject("category" , categoryService.selectCategory());
        modelAndView.setViewName("category");
        return modelAndView;
    }

    @GetMapping("/getCategoryInf")
    @ResponseBody
    public Category getCategoryInf(HttpServletRequest req){
        String categoryId = req.getParameter("categoryId");
        return categoryService.selectCategoryById(categoryId);
    }

    @PostMapping("/upload")
    @ResponseBody
    public String handleFileUpload(MultipartFile image, @RequestHeader("category") String category) throws IOException {
        // 对上传的文件进行处理 @RequestHeader("product") String product
        ModelAndView modelAndView = new ModelAndView();
        try {
            modelAndView.setViewName("product_manager");
            byte[] fileBytes = image.getBytes();
            String head_inf = "data:image/png;base64,";
            String encodedFile = Base64.getEncoder().encodeToString(fileBytes);
            String result = head_inf + encodedFile;
            System.out.println("这个productId" + category);
            categoryService.updateCategoryPic(category,result);
            modelAndView.addObject("product",categoryService.selectCategory());
            return "success";
        } catch (IOException e) {
            System.out.println("文件转码失败");
            modelAndView.setViewName("product_manager");
            modelAndView.addObject("product",categoryService.selectCategory());
            return "success";
        }
    }

    @DeleteMapping("/deleteCategory")
    @ResponseBody
    public String deleteProduct(@RequestParam("category") String categoryId) {
        categoryService.deleteCategory(categoryId);
        return "delete category!";
    }

}
